package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lucher.mes.common.DataUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.DeviceMapper;
import com.lucher.mes.dao.DevicedepotMapper;
import com.lucher.mes.dao.DeviceinoutMapper;
import com.lucher.mes.po.Coderule;
import com.lucher.mes.po.Device;
import com.lucher.mes.po.Devicedepot;
import com.lucher.mes.po.Deviceinout;
import com.lucher.mes.po.Operator;
import com.lucher.mes.service.ICoderuleService;

@Controller
@RequestMapping("/device")  
public class DeviceController extends BaseController{
	@Autowired
	private DeviceMapper deviceDao;
	@Autowired
	private ICoderuleService coderuleService;
	@Autowired
	private DevicedepotMapper devicedepotDao;
	@Autowired
	private DeviceinoutMapper deviceinoutDao;
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Device form) {
		return "device/findDeviceList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Device form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = deviceDao.findByCount(form);
		List<Device> list = deviceDao.findByListForPage(form);
		
		for (Device device : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("id", device.getId());
			objectMap.put("devicecode", device.getDevicecode());
			objectMap.put("devicename", device.getDevicename());
			Devicedepot record = new Devicedepot();
			record.setDevicecode(device.getDevicecode());
			//查询总数
			Integer totalnum = devicedepotDao.findByCount(record);
			objectMap.put("totalnum", totalnum);
			//查询领用总数
			record.setDepotstatus("使用中");
			Integer usenum = devicedepotDao.findByCount(record);
			objectMap.put("usenum", usenum);
			//查询维修总数
			record.setDepotstatus("维修中");
			Integer repairnum = devicedepotDao.findByCount(record);
			objectMap.put("repairnum", repairnum);
			//查询库存总数
			record.setDepotstatus("在库");
			Integer depotnum = devicedepotDao.findByCount(record);
			objectMap.put("depotnum", depotnum);
			objectMap.put("remark", device.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/buyInInit")
	public String buyInInit(Device form) {
		return "device/buyIn";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/saveBuyIn")
	public Map<String,Object>  saveBuyIn(Device form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		
		if ("".equals(form.getDevicename())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备名称");
			return responseJson;
		}
		
		if ("".equals(form.getDevicecode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备型号");
			return responseJson;
		} else {
			Device oldDevice = deviceDao.findByDevicecode(form.getDevicecode());
			if (oldDevice != null) { //修改当前的设备库存量
				oldDevice.setDevicename(form.getDevicename());
				oldDevice.setTotalnum(oldDevice.getTotalnum() + form.getDepotamount());
				deviceDao.updateByPrimaryKeySelective(oldDevice);
			}else{//新增设备入库
				//保存设备信息
				form.setUsenum(0);
				form.setRepairnum(0);
				form.setTotalnum(form.getDepotamount());
				Integer deviceId = deviceDao.insertSelective(form);
			}
		}
		
		//保存每个设备的库存详情
		String batchno = coderuleService.getSystemcodenoByCodetypeStr(Coderule.CODETYPE_devicecode);
		ArrayList<Devicedepot> devicedepotList = new ArrayList<Devicedepot>();
		ArrayList<Deviceinout> deviceinoutList = new ArrayList<Deviceinout>();
		for (int i = 0; i < form.getDepotamount(); i++) {
			batchno = String.valueOf(Long.valueOf(batchno) + i);
			//设备库存
			Devicedepot devicedepot = new Devicedepot();
			devicedepot.setDevicecode(form.getDevicecode());
			devicedepot.setDevicename(form.getDevicename());
			devicedepot.setBatchno(batchno);
			devicedepot.setDepotamount(1);
			devicedepot.setDepotstatus("在库");
			devicedepot.setAddtime(currenttime);
			devicedepot.setInfo(form.getInfo());
			devicedepotList.add(devicedepot);
			
			//增加设备入库记录
			Deviceinout devicelinout = new Deviceinout();
			devicelinout.setDevicecode(form.getDevicecode());
			devicelinout.setDevicename(form.getDevicename());
			devicelinout.setBatchno(batchno);
			devicelinout.setType("入库");
			devicelinout.setTypeinfo("采购入库");
			devicelinout.setAddtime(currenttime);
			devicelinout.setOperatorcode(operator.getLoginname());
			deviceinoutList.add(devicelinout);
		}
		//更新数据库最新的批次号
		coderuleService.updateSystemcodenoByCodetypeStr(Coderule.CODETYPE_devicecode,batchno);
		//批量保存库存
		devicedepotDao.saveBatch(devicedepotList);
		//批量保存入库记录
		deviceinoutDao.saveBatch(deviceinoutList);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/useOutInit")
	public String useOutInit(Device form) {
		return "device/useOut";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/saveUseOut")
	@Transactional
	public Map<String,Object>  saveUseOut(Device form,Devicedepot depotForm) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		
		String batchnos = StringUtils.trim(form.getBatchnos());
		
		if ("".equals(batchnos)) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		if ("".equals(depotForm.getUsearea())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入领用监区");
			return responseJson;
		}

		//获取数输入信息 格式化未数组 并对数组进行去重
		String[] batchArr = DataUtils.arrayCleanByMap(batchnos.split("\n") );

		if(batchArr == null || batchArr.length == 0){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot == null){//不存在库存
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备在库存中不存在！");
				return responseJson;
			}
			if(!"在库".equals(devicedepot.getDepotstatus())){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备不是在库状态！");
				return responseJson;
			}
			Device device  = deviceDao.findByDevicecode(devicedepot.getDevicecode());
			if(device == null){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的对应的设备型号在不存在！");
				return responseJson;
			}
		}
		
		ArrayList<Deviceinout> deviceinoutList = new ArrayList<Deviceinout>();
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot != null){
				devicedepot.setDepotstatus("使用中");
				devicedepot.setUsearea(depotForm.getUsearea());
				devicedepot.setUsetime(currenttime);
				devicedepotDao.updateByPrimaryKeySelective(devicedepot);
			}
			//增加设备入库记录
			Deviceinout devicelinout = new Deviceinout();
			devicelinout.setDevicecode(devicedepot.getDevicecode());
			devicelinout.setDevicename(devicedepot.getDevicename());
			devicelinout.setBatchno(batchno);
			devicelinout.setType("出库");
			devicelinout.setTypeinfo("领用出库");
			devicelinout.setAddtime(currenttime);
			devicelinout.setOperatorcode(operator.getLoginname());
			deviceinoutList.add(devicelinout);
		}
		
		//批量保存入库记录
		deviceinoutDao.saveBatch(deviceinoutList);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/returnInInit")
	public String returnInInit(Device form) {
		return "device/returnIn";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/saveReturnIn")
	@Transactional
	public Map<String,Object>  saveReturnIn(Device form,Devicedepot depotForm) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		
		String batchnos = StringUtils.trim(form.getBatchnos());
		
		if ("".equals(batchnos)) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		String[] batchArr = DataUtils.arrayCleanByMap(batchnos.split("\n") );
		if(batchArr == null || batchArr.length == 0){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot == null){//不存在库存
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备在库存中不存在！");
				return responseJson;
			}
			Device device  = deviceDao.findByDevicecode(devicedepot.getDevicecode());
			if(device == null){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的对应的设备型号在不存在！");
				return responseJson;
			}
		}
		
		ArrayList<Deviceinout> deviceinoutList = new ArrayList<Deviceinout>();
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot != null){
				devicedepot.setDepotstatus("在库");
				devicedepot.setUsearea(null);
				devicedepot.setUsetime(null);
				devicedepotDao.updateByPrimaryKey(devicedepot);
			}
			//增加设备入库记录
			Deviceinout devicelinout = new Deviceinout();
			devicelinout.setDevicecode(devicedepot.getDevicecode());
			devicelinout.setDevicename(devicedepot.getDevicename());
			devicelinout.setBatchno(batchno);
			devicelinout.setType("入库");
			devicelinout.setTypeinfo("归还入库");
			devicelinout.setAddtime(currenttime);
			devicelinout.setOperatorcode(operator.getLoginname());
			deviceinoutList.add(devicelinout);
		}
		
		//批量保存入库记录
		deviceinoutDao.saveBatch(deviceinoutList);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	
	/**
	 * 添加维修出库初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/repairOutInit")
	public String repairOutInit(Device form) {
		return "device/repairOut";
	}

	/**
	 * 保存维修出库
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/saveRepairOut")
	@Transactional
	public Map<String,Object>  saveRepairOut(Device form,Devicedepot depotForm) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		
		String batchnos = StringUtils.trim(form.getBatchnos());
		
		if ("".equals(batchnos)) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		String[] batchArr =  DataUtils.arrayCleanByMap(batchnos.split("\n") );
		if(batchArr == null || batchArr.length == 0){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot == null){//不存在库存
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备在库存中不存在！");
				return responseJson;
			}
			Device device  = deviceDao.findByDevicecode(devicedepot.getDevicecode());
			if(device == null){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的对应的设备型号在不存在！");
				return responseJson;
			}
		}
		
		ArrayList<Deviceinout> deviceinoutList = new ArrayList<Deviceinout>();
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot != null){
				devicedepot.setDepotstatus("维修中");
				devicedepot.setUsearea(null);
				devicedepot.setUsetime(null);
				devicedepotDao.updateByPrimaryKeySelective(devicedepot);
			}
			//增加设备入库记录
			Deviceinout devicelinout = new Deviceinout();
			devicelinout.setDevicecode(devicedepot.getDevicecode());
			devicelinout.setDevicename(devicedepot.getDevicename());
			devicelinout.setBatchno(batchno);
			devicelinout.setType("出库");
			devicelinout.setTypeinfo("维修出库");
			devicelinout.setAddtime(currenttime);
			devicelinout.setOperatorcode(operator.getLoginname());
			deviceinoutList.add(devicelinout);
		}
		
		//批量保存入库记录
		deviceinoutDao.saveBatch(deviceinoutList);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 添加维修入库初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/repairInInit")
	public String repairInInit(Device form) {
		return "device/repairIn";
	}

	/**
	 * 维修入库
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/saveRepairIn")
	@Transactional
	public Map<String,Object>  saveRepairIn(Device form,Devicedepot depotForm) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		
		String batchnos = StringUtils.trim(form.getBatchnos());
		
		if ("".equals(batchnos)) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		String[] batchArr =  DataUtils.arrayCleanByMap(batchnos.split("\n") );
		if(batchArr == null || batchArr.length == 0){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot == null){//不存在库存
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备在库存中不存在！");
				return responseJson;
			}
			
			if(!"维修中".equals(devicedepot.getDepotstatus())){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备不是维修中状态！");
				return responseJson;
			}
			
			Device device  = deviceDao.findByDevicecode(devicedepot.getDevicecode());
			if(device == null){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的对应的设备型号在不存在！");
				return responseJson;
			}
		}
		
		ArrayList<Deviceinout> deviceinoutList = new ArrayList<Deviceinout>();
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot != null){
				devicedepot.setDepotstatus("在库");
				devicedepot.setUsearea(null);
				devicedepot.setUsetime(null);
				devicedepotDao.updateByPrimaryKey(devicedepot);
			}
			//增加设备入库记录
			Deviceinout devicelinout = new Deviceinout();
			devicelinout.setDevicecode(devicedepot.getDevicecode());
			devicelinout.setDevicename(devicedepot.getDevicename());
			devicelinout.setBatchno(batchno);
			devicelinout.setType("入库");
			devicelinout.setTypeinfo("维修入库");
			devicelinout.setAddtime(currenttime);
			devicelinout.setOperatorcode(operator.getLoginname());
			deviceinoutList.add(devicelinout);
		}
		
		//批量保存入库记录
		deviceinoutDao.saveBatch(deviceinoutList);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 添加报废出库初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/delOutInit")
	public String delOutInit(Device form) {
		return "device/delOut";
	}

	/**
	 * 保存报废出库
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/saveDelOut")
	@Transactional
	public Map<String,Object>  saveDelOut(Device form,Devicedepot depotForm) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		
		String batchnos = StringUtils.trim(form.getBatchnos());
		
		if ("".equals(batchnos)) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		String[] batchArr = DataUtils.arrayCleanByMap(batchnos.split("\n") );
		if(batchArr == null || batchArr.length == 0){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入设备编号");
			return responseJson;
		}
		
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			if(devicedepot == null){//不存在库存
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的设备在库存中不存在！");
				return responseJson;
			}
			Device device  = deviceDao.findByDevicecode(devicedepot.getDevicecode());
			if(device == null){
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，设备编号为：" + batchno +" 的对应的设备型号在不存在！");
				return responseJson;
			}
		}
		
		ArrayList<Deviceinout> deviceinoutList = new ArrayList<Deviceinout>();
		for (int i = 0; i < batchArr.length; i++) {
			String batchno = batchArr[i];
			Devicedepot devicedepot = devicedepotDao.findByBatchno(batchno);
			//报废删除
			devicedepotDao.deleteByPrimaryKey(devicedepot.getId());
			//增加设备入库记录
			Deviceinout devicelinout = new Deviceinout();
			devicelinout.setDevicecode(devicedepot.getDevicecode());
			devicelinout.setDevicename(devicedepot.getDevicename());
			devicelinout.setBatchno(batchno);
			devicelinout.setType("出库");
			devicelinout.setTypeinfo("报废出库");
			devicelinout.setAddtime(currenttime);
			devicelinout.setOperatorcode(operator.getLoginname());
			deviceinoutList.add(devicelinout);
		}
		
		//批量保存入库记录
		deviceinoutDao.saveBatch(deviceinoutList);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Device form) {
		Device device = deviceDao.selectByPrimaryKey(form.getId());
		form.setDevice(device);

		return "device/updateDevice";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Device form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getDevicecode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，设备编码不能为空");
			return responseJson;
		} else {
			Device oldDevice = deviceDao.findByDevicecode(form.getDevicecode());
			if (oldDevice != null && !oldDevice.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，设备编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getDevicename())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，设备名称不能为空");
			return responseJson;
		} else {
			Device oldDevice = deviceDao.findByDevicename(form.getDevicename());
			if (oldDevice != null && !oldDevice.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，设备名称已经存在");
				return responseJson;
			}
		}

		// 修改信息
		int flag = deviceDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Device form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除设备
		deviceDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Device> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Device device : formlist) {
			//删除设备
			deviceDao.deleteByPrimaryKey(device.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
     * 获取设备的下拉列表框Json值
     */
	@ResponseBody //此标签表示返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	@RequestMapping(value="/getDeviceComboBoxJson")
	public List<Map<String,Object>> getDeviceComboBoxJson(Device form) {  
	    List<Device> deviceList = deviceDao.findByList(form);
	    List<Map<String, Object>> deviceComboBoxJson = new ArrayList<Map<String, Object>>();
	    //添加请选择项-默认为空，查询所有
	    HashMap<String,Object> selectMap = new HashMap<String, Object>();
	    selectMap.put("id", "");
	    selectMap.put("text", "请选择");
	    deviceComboBoxJson.add(selectMap);
	    
	    //封装区域属性结构信息
	    for(Device device:deviceList){  
    	    HashMap<String,Object> deviceMap = new HashMap<String, Object>();
    	    deviceMap.put("id", device.getDevicecode());
    	    deviceMap.put("text", device.getDevicename()+"("+device.getDevicecode()+")");
    	    
    	    deviceComboBoxJson.add(deviceMap);  
	    }  
	   return deviceComboBoxJson;
	}  
	
	/**
	 * 查询材料库存
	 */
	@RequestMapping(value = "/findDevicedepotByList")
	public String findDevicedepotByList(Devicedepot form) {
		Device device = deviceDao.selectByPrimaryKey(form.getId());
		form.setDevicecode(device.getDevicecode());
		form.setDevicename(device.getDevicename());
		return "device/findDevicedepotList";
	}
	
}
